package com.lanou3g.baidumusic.main.musicfragment.recommendfragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.lanou3g.baidumusic.R;
import com.lanou3g.baidumusic.main.mainactivity.BaseFragment;
import com.lanou3g.baidumusic.main.tools.NetResultInterface;
import com.lanou3g.baidumusic.main.tools.NetWorkInstance;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends BaseFragment {

    private RecyclerView recyclerView ;
    private RecommendRecyclerViewAdapter adapter ;

    private String  urlStrs ="http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.9.9.3&channel=xiaomi&operator=1&method=baidu.ting.plaza.index&cuid=F7A8FC209E8009122DBF740A66CDA817&focu_num=8%20HTTP/1.1";

    public RecommendFragment() {
        // Required empty public constructor
    }


    @Override
    public int setLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initView(View view) {

        recyclerView = (RecyclerView) view.findViewById(R.id.fragment_recommend_rv_rv);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext() , LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);

    }

    @Override
    public void initData() {

        adapter = new RecommendRecyclerViewAdapter(getContext());

        recyclerView.setAdapter(adapter);


        NetWorkInstance.getInstance().netRequest(urlStrs, new NetResultInterface() {
            //当网络请求成功了,自动调用此方法
            @Override
            public void sucess(String str) {
//                Log.d("success", str);

                //gson解析
                Gson gson = new Gson();
                //解析后的数据存于homeEntity中
                RecommendRecyclerViewBean bean = gson.fromJson(str , RecommendRecyclerViewBean.class);
//                homeEntity.getData().getItems().get(1).getIntroduction();
                adapter.setRecommendRecyclerViewBean(bean);

            }
            //网络请求失败了,自动调用次方法
            @Override
            public void faile(String str) {
                Log.d("faile" + "", str);
            }
        });

    }
}
